package cn.com.generaldata.os.ws.impl;

import java.util.List;

import javax.jws.WebService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import cn.com.generaldata.jsme_indicator_system.entity.IndicatorProvince;
import cn.com.generaldata.os.result.IndicatorProvinceCategoryWSResult;
import cn.com.generaldata.os.result.IndicatorStatWSResult;
import cn.com.generaldata.os.result.WSResult;
import cn.com.generaldata.os.service.impl.IndicatorStatService;
import cn.com.generaldata.os.vo.IndicatorProvinceVO;
import cn.com.generaldata.os.vo.IndicatorStatQueryVO;
import cn.com.generaldata.os.vo.IndicatorStatResult;
import cn.com.generaldata.os.ws.intf.IndicatorStatWSInterface;

import com.google.common.collect.Lists;

@WebService(endpointInterface = "cn.com.generaldata.os.ws.intf.IndicatorStatWSInterface")
public class IndicatorStatWS implements IndicatorStatWSInterface {

	private static Logger logger = LoggerFactory.getLogger(IndicatorStatWS.class);

	@Autowired
	private IndicatorStatService indicatorStatService;

	@Override
	public IndicatorStatWSResult indicatorStat(IndicatorStatQueryVO queryVO) {
		IndicatorStatWSResult result = new IndicatorStatWSResult();
		try {
			List<IndicatorStatResult> statResult = indicatorStatService.indicatorStat(queryVO);
			result.setResult(statResult);

		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = handleGeneralError(result, e);
		}

		return result;
	}

	@Override
	public IndicatorProvinceCategoryWSResult getIndicatorCategory(Integer parentId) {
		IndicatorProvinceCategoryWSResult result = new IndicatorProvinceCategoryWSResult();

		try {
			List<IndicatorProvince> indicators = indicatorStatService.getIndicatorCategory(parentId);
			List<IndicatorProvinceVO> vos = Lists.newArrayList();
			for (IndicatorProvince indicator : indicators) {
				IndicatorProvinceVO vo = new IndicatorProvinceVO();
				vo.setId(indicator.getId());
				vo.setName(indicator.getName());
				vo.setCode(indicator.getCode());
				vos.add(vo);
			}
			result.setIndicators(vos);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = handleGeneralError(result, e);
		}
		return result;
	}

	private <T extends WSResult> T handleGeneralError(T result, Exception e) {
		logger.error(e.getMessage(), e);
		result.setDefaultError();
		return result;
	}

}
